import React from "react";
import {ErrorBlock, Toast} from "antd-mobile";
import cookie from "_react-cookies@0.1.1@react-cookies";


export default class CheckLogin extends React.Component {


    constructor(props) {
        super(props);
        this.state = {
            showElem: false,
            code:'',
            phone:'',

        }
    }

    componentDidMount() {
        document.title='登陆验证'
        this.setState({phone:cookie.load("phone")})
        //获取跳转后接受到的code
        const str = this.props.location.search;
        const pattern = /=(.+)&/;
        const code = pattern.exec(str)[1]
        console.log(code)



        //TODO 获取用户手机号，判断是否跳转


        //通过微信的api 获取用户的 信息
        const url ='http://htjoa.nrnet.cn/dc14/construct/wechat/phone?code='+code;
        fetch(url,{method:"GET"
        })
            .then(res=>res.json()).then(result=> {
            if (result["code"] == "200") {
                console.log(result)
                cookie.save("phone", result["phone"]);
                cookie.save("name",result["name"]);
                cookie.save("avatar",result["avatar"]);
                this.props.history.push('/mainpage')
            }
        })





        // const url = 'http://htjoa.nrnet.cn/dc14/construct/login/phone';
        // const url1 = 'http://47.102.200.101:9003/dc14/construct/login/phone';
        // const url2 = 'http://htjoa.nrnet.cn/dc14/construct/login/login';
        // fetch(url2, {
        //     method: "GET"
        // })
        //     .then(res => res.json()).then(result => {
        //     console.log(result)
        //     if (result["code"] == "200") {
        //         const phoneNumber = result["phone"]
        //         console.log(phoneNumber)
        //         // appContext.
        //         this.props.history.push('/dc14/const/index')
        //
        //     } else {
        //         this.setState({showElem: true})
        //     }
        // }).catch(
        //
        // )

    }

    render() {
        return (
            <div>

                {
                    this.state.showElem ? (
                        <ErrorBlock fullPage status='default'
                                    description={
                                        <p>
                                            未找到用户信息，请联系管理员。
                                        </p>
                                    }></ErrorBlock>
                    ) :<div>{this.state.code}</div>
                }

            </div>
        )
    }
}
